import { BaseEntity } from "src/common/entities/base.entity";
import {
  Column,
  Entity,
  JoinColumn,
  JoinTable,
  ManyToMany,
  ManyToOne,
  PrimaryGeneratedColumn,
} from "typeorm";
import { Type } from "class-transformer";
import { IsNumber, IsOptional, IsString, IsEmail, IsPhoneNumber, Allow } from "class-validator";
import { ApiHideProperty } from "@nestjs/swagger";
import { Dept } from "../../dept/entities/dept.entity";
import { Role } from "../../role/entities/role.entity";

@Entity()
export class User extends BaseEntity {
  /** 用户id */
  @PrimaryGeneratedColumn({ name: "userId", comment: "用户id" })
  @Type()
  @IsNumber()
  userId: number;

  @Column({
    name: "username",
    comment: "用户账号",
    length: 30,
  })
  @IsString()
  username: string;

  @Column({
    name: "nick_name",
    comment: "用户昵称",
    length: 30,
  })
  @IsString()
  nick_name: string;

  @Column({
    comment: "密码",
    length: 100,
    default: "",
    select: false,
  })
  @IsString()
  password: string;

  @Column({
    name: "remark",
    comment: "备注",
    length: 255,
    default: "",
  })
  @IsString()
  @IsOptional()
  remark?: string;

  @ApiHideProperty()
  @Column({
    comment: "盐加密",
    length: 100,
    default: "",
    select: false,
  })
  salt: string;

  @ApiHideProperty()
  @Column({
    name: "del_flag",
    comment: "删除标志（0代表存在 2代表删除）",
    type: "char",
    length: 1,
    default: "0",
  })
  delFlag: string;

  @ApiHideProperty()
  @ManyToOne(() => Dept, (dept) => dept.users)
  @JoinColumn({ name: "dept_id" })
  dept: Dept;

  @ApiHideProperty()
  @ManyToMany(() => Role, (role) => role.users)
  @JoinTable()
  roles: Role[];
}
